<!-- print.view -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
	<title>Using JUnit With Eclipse IDE - O'Reilly Media</title>
	<meta name="keywords" content="JUnit,Eclipse,test-driven development,XP,O'Reilly Media" />
	<meta name="description" content="Demonstration of test-first development with JUnit and Eclipse" />
	<meta name="date" content="Feb. 04, 2004" />
	<meta name="search_date" content="2004-02-04" />
	<meta name="pagename" content="Using JUnit With Eclipse IDE" />
	<meta name="site" content="ONJava.com" />
	<meta name="subtitle" content="" />
	<meta name="author" content="Alexander Prohorenko" />
<meta name="target" content="" />


<!-- feed autodiscovery links -->
<link rel="alternate" type="application/atom+xml" title="Java News and Articles" href="http://feeds2.feedburner.com/oreilly/java" />

<!-- sc/388 Common JavaScript -->
<script language="JavaScript" type="text/javascript" src="http://www.oreillynet.com/common.js"></script>

<script language="JavaScript" type="text/javascript" src="http://content.atomz.com/pb000035c5/publish/assets/js/common.js"></script>

<link rel="STYLESHEET" type="text/css" href="http://www.oreillynet.com/styles/main.css" />
<!-- path_info: /a/onjava/2004/02/04/juie.html -->
<!-- table: a - ref_id: /onjava/2004/02/04/juie.html -->
<!-- table: a - ref_id: /onjava/2004/02/04/juie.html -->
<!-- table: a - ref_id: /onjava/2004/02/04/juie.html - cs_rid: 4524 -->
<!-- 0 domain: www.onjava.com - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 1 domain: www.onjava.com - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 2 domain: www.onjava.com - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 3 domain: www.onjava.com - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 4 domain: onjava.com - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 5 domain: onjava - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 6 domain: onjava - path_info: a/onjava/2004/02/04/juie.html s_channel: network -->
<!-- 7 domain: onjava - path_info: a/onjava/2004/02/04/juie.html -->
<!-- 8 domain: www.onjava.com - path_info: a/onjava/2004/02/04/juie.html -->

<script language="JavaScript" type="text/javascript"><!--
var s_account="ornetwork,orglobal"
//--></script>
<!-- SiteCatalyst code version: H.20.2. Copyright 1997-2009 Omniture, Inc. More info available at http://www.omniture.com  -->
<script language="JavaScript" type="text/javascript" src="http://assets.oreilly.com/js/s_code.js"></script>
<script language="JavaScript" type="text/javascript"><!--
s.pageName="network:onjava:using junit with eclipse ide"
s.channel="network"
s.prop1="network:onjava"
s.prop2="network:onjava"
s.prop3="network:onjava"
s.prop4="article"
s.prop5="using junit with eclipse ide"
s.prop6="n/a/4524"
s.prop24="02/04/2004"
s.prop21="alexander prohorenko,olexiy prokhorenko"
s.hier1="network,onjava"
/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
var s_code=s.t();if(s_code)document.write(s_code)//--></script>
<!-- End SiteCatalyst code version: H.20.2. -->
<!-- analytics.pl: 2011/8/8 20:00:59 -->



<script type="text/javascript" src="http://www.oreillynet.com/tabber.js"></script>

<!-- additional css -->
<link rel="stylesheet" type="text/css" href="http://www.oreillynet.com/styles/articles.css" />

<!-- MyBuys libraries and style sheet -->
<link href="http://t.p.mybuys.com/css/mbstyles.css" type="text/css" rel="stylesheet" id="mybuysstyles" />
<script type="text/javascript" src="http://t.p.mybuys.com/js/mybuys3.js"></script>
<script type="text/javascript" src="http://t.p.mybuys.com/clients/OREILLY/js/setup.js"></script>
<!-- End MyBuys libraries and style sheet -->

</head>
<body id="onjava">
<div id="page">
<div id="header" class="noindex">
<h1><a href="http://www.onjava.com/"><img src="/images/onjava/onjava_logo.jpg" width="294" height="82" border="0" alt="ONJava.com -- The Independent Source for Enterprise Java" hspace="0" vspace="0" /></a></h1>

</div> 


<!-- tabs 
<csinclude record="sc/1097" field="html" />
-->


<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" valign="bottom">
<a href="/cs/user/login?x-redirect=/cs/user/home" class="tiny">Sign In/My Account</a>&nbsp;|&nbsp;<a href="http://epoch.oreilly.com/shop/cart.orm" class="tiny">View Cart</a>&nbsp;&nbsp;</td>
</tr></table>


<div id="newheaders">

<br />
<div id="newHead">


<link rel="stylesheet" rev="stylesheet" href="http://www.oreillynet.com/styles/all.css" type="text/css" />
<link rel="stylesheet" rev="stylesheet" href="http://www.oreillynet.com/styles/about-121707.css" />

<style type="text/css">

#search-box {
background-color:#F6F6F6;
border-bottom:1px solid #CCCCCC;
padding-top:11px;
text-align:center;
}
#search-form {
margin:0pt auto;
text-align:left;
width:400px;
}
#search-title {
color:#888888;
display:block;
float:left;
font-size:17px;
padding:5px 5px 4px 0pt;
}
#search-input {
background:transparent url(http://www.oreilly.com/images/oreilly/search_bar_mid.gif) repeat-y scroll left top;
display:block;
float:left;
padding:0pt 5px 0pt 0pt;
width:260px;
}
#search-input-top {
background:transparent url(http://www.oreilly.com/images/oreilly/search_bar_top.gif) no-repeat scroll left top;
display:block;
width:260px;
}
#search-input-bot {
background:transparent url(http://www.oreilly.com/images/oreilly/search_bar_bot.gif) no-repeat scroll left bottom;
display:block;
width:260px;
}
#search-field {
float:left;
}
#search-field input {
background:transparent none repeat scroll 0%;
border:0pt none;
color:#990000;
font-family:Verdana,Arial,Helvetica,sans-serif;
font-size:16px;
margin:0pt;
padding:3px 0pt 4px 10px;
width:215px;
}
#search-field input:focus {
outline-color:invert;
outline-style:none;
outline-width:0pt;
}
#search-button {
float:right;
}
#search-button input {
font-size:0pt;
height:18px;
line-height:0pt;
margin:4px 4px 2px 0pt;
padding:0pt;
width:28px;
}
#search-tips {
display:block;
float:left;
font-size:10px;
padding:6px 0pt 0pt;
}
#search label {
text-transform:uppercase;
}
#search legend {
display:none;
}
#search label {
left:-9999px;
position:absolute;
}
fieldset {
border:0pt none;
margin:0pt;
padding:0pt;
}
.clear {
display:block;
line-height:0pt;
}
#wrapper {
margin:1em 0pt 2em 19em;
overflow:hidden;
}
#features {
float:left;
margin:0pt;
padding:0pt 2em 0pt 0pt;
width:41em;

a{text-decoration:none;} 

#search-box{

padding-top: 11px;

padding-bottom: 0pt;

background-color: #f6f6f6;

border-bottom-width: 1px;

border-top-style: solid;

border-top-width: 1px;

border-top-color: #cccccc;

border-bottom-style: solid;

border-bottom-color: #cccccc;

text-align: center;

}

</style>


<div id="nav" class="noindex">
<ul>
<li><a href="http://oreilly.com/">O'Reilly Home</a></li>
<li><a href="http://community.oreilly.com/">Community</a></li>
<li><a href="http://oreilly.com/store/">Books & Videos</a></li>
<li><a href="http://safari.oreilly.com/?cid=orm-nav-global">Safari Books Online</a></li>
<li><a href="http://conferences.oreillynet.com/">Conferences</a></li>
<li><a href="http://training.oreilly.com/">Training</a></li>
<li><a href="http://www.oreillylearning.com">School of Technology</a></li>
<li><a href="http://oreilly.com/about/">About</a></li>

</ul>
<div class="clear"></div>
</div><!-- /#nav -->







<div id="newsearch">


<style type="text/css">

#search-box {
background-color:#F6F6F6;
border-bottom:1px solid #CCCCCC;
padding-top:11px;
text-align:center;
}
#search-form {
margin:0pt auto;
text-align:left;
width:400px;
}
#search-title {
color:#888888;
display:block;
float:left;
font-size:17px;
padding:5px 5px 4px 0pt;
}
#search-input {
background:transparent url(http://www.oreilly.com/images/oreilly/search_bar_mid.gif) repeat-y scroll left top;
display:block;
float:left;
padding:0pt 5px 0pt 0pt;
width:260px;
}
#search-input-top {
background:transparent url(http://www.oreilly.com/images/oreilly/search_bar_top.gif) no-repeat scroll left top;
display:block;
width:260px;
}
#search-input-bot {
background:transparent url(http://www.oreilly.com/images/oreilly/search_bar_bot.gif) no-repeat scroll left bottom;
display:block;
width:260px;
}
#search-field {
float:left;
}
#search-field input {
background:transparent none repeat scroll 0%;
border:0pt none;
color:#990000;
font-family:Verdana,Arial,Helvetica,sans-serif;
font-size:16px;
margin:0pt;
padding:3px 0pt 4px 10px;
width:215px;
}
#search-field input:focus {
outline-color:invert;
outline-style:none;
outline-width:0pt;
}
#search-button {
float:right;
}
#search-button input {
font-size:0pt;
height:18px;
line-height:0pt;
margin:4px 4px 2px 0pt;
padding:0pt;
width:28px;
}
#search-tips {
display:block;
float:left;
font-size:10px;
padding:6px 0pt 0pt;
}
#search label {
text-transform:uppercase;
}
#search legend {
display:none;
}
#search label {
left:-9999px;
position:absolute;
}
fieldset {
border:0pt none;
margin:0pt;
padding:0pt;
}
.clear {
display:block;
line-height:0pt;
}
#wrapper {
margin:1em 0pt 2em 19em;
overflow:hidden;
}
#features {
float:left;
margin:0pt;
padding:0pt 2em 0pt 0pt;
width:41em;

a{text-decoration:none;} 

#search-box{

padding-top: 11px;

padding-bottom: 0pt;

background-color: #f6f6f6;

border-bottom-width: 1px;

border-top-style: solid;

border-top-width: 1px;

border-top-color: #cccccc;

border-bottom-style: solid;

border-bottom-color: #cccccc;

text-align: center;

}

</style>



<div id="search-box" class="yui-skin-sam">

            <form method="get" action="http://search.oreilly.com" id="search-form" name="searchfield">
                <fieldset>



                    <span id="search-title" style="color:#474747">Search</span>
                    <span id="search-input">
                        <span id="search-input-top">
                            <span id="search-input-bot">
                                <span id="search-field"><input style="//height: 25px;" id="q" name="q" type="text" maxlength="64" accesskey="s" value="" /></span>
                                <span id="search-button"><input type="image" name="submit" src="http://www.oreilly.com/images/oreilly/search_bar_btn.gif" alt="Submit" onclick="return searchverif();" /></span>
                                <span class="clear"></span>
                            </span>
    </span>
                    </span>
                            <div id="autocomplete"></div>
                   
                    <span id="search-tips"><a href="http://www.oreillynet.com/search/tips.csp" target="_blank">Search Tips</a></span>
                </fieldset>
            </form>

<br />
        </div><!-- /#search-box -->
</center>
</div>


</div>

</div>


<!-- menubar
<csinclude record="sc/1117" field="html" />
-->
<!--
<csinclude record="sc/2032" field="html" />
-->

<div id="leaderboard">


<img src="http://www.oreillynet.com/images/ads/ad-tag-vertical.gif" width="11" height="90" border="0" alt="advertisement" style="float:right;"/>


<div style="width:728px;margin:0 auto;">

<!--JavaScript Tag // Tag for network 5159: TechVertical // Website: OR - ONJava // Page: ONJava // Placement: ros_728x90 (425854) // created at: Oct 9, 2008 5:48:42 PM-->
<script language="javascript"><!--
document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5159/425854/0/225/ADTECH;loc=100;target=_blank;key=key1+key2+key3+key4;grp=[group];misc='+new Date().getTime()+'"></scri'+'pt>');
//-->
</script><noscript><a href="http://adserver.adtechus.com/adlink/3.0/5159/425854/0/225/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" target="_blank"><img src="http://adserver.adtechus.com/adserv/3.0/5159/425854/0/225/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" border="0" width="728" height="90"></a></noscript>

<!-- End of JavaScript Tag -->
</div>

</div> <!-- end leaderboard -->

<div id="textbar"></div>
<!-- </div>  end header -->


<div id="page-content">
<div id="article-onjava">

<!-- feature bar ad : layout component 1894 -->

<div align="center" style="visibility: hidden">
<!-- begin ad tag -->

<!--JavaScript Tag // Tag for network 5159: TechVertical // Website: O'ReillyNet // Page: O'ReillyNet // Placement: text (425846) // created at: Oct 9, 2008 6:10:33 PM-->
<script language="javascript"><!--
document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5159/425846/0/16/ADTECH;loc=100;target=_blank;key=key1+key2+key3+key4;grp=[group];misc='+new Date().getTime()+'"></scri'+'pt>');
//-->
</script><noscript><a href="http://adserver.adtechus.com/adlink/3.0/5159/425846/0/16/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" target="_blank"><img src="http://adserver.adtechus.com/adserv/3.0/5159/425846/0/16/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" border="0" width="1" height="1"></a></noscript>
<!-- End of JavaScript Tag -->


<!-- End ad tag -->
</div>

<!-- end feature bar ad -->
<br />



<a href="http://www.addthis.com/bookmark.php" onclick="addthis_url   = location.href; addthis_title = document.title; return addthis_click(this);" target="_blank"><img src="http://s5.addthis.com/button1-bm.gif" width="125" height="16" border="0" alt="AddThis Social Bookmark Button" /></a> <script type="text/javascript">var addthis_pub = 'odewahn';</script><script type="text/javascript" src="http://s5.addthis.com/js/widget.php?v=10"></script>  

<p>

<!-- begin content-->









<!-- begin  print-discuss-del.icio.us options -->
<div style="margin: 0px 10px 0px 6px;">
<div style="float: right; width: 170px;">

<a class="print" title="Go to a print-friendly version of this page." href="/lpt/a/4524">Print</a>
<!--csif cond="yes">
<a class="discuss" title="Join the talkback discussion about this article" href="/pub/a/onjava/2004/02/04/juie.html?page=last#thread">Discuss</a>
</csif-->









<a class="subscribe" title="Subscribe" href="http://www.oreillynet.com/pub/feed/7">Subscribe to ONJava</a>














<a class="newsletter" href="http://www.oreillynet.com/cs/nl/home">Subscribe to Newsletters</a>

<div id="share-options" style="text-align:right;">
<script type="text/javascript" src="http://w.sharethis.com/button/sharethis.js#publisher=16098316-763b-41fc-a31d-7bbadcfcf894&amp;type=website&amp;embeds=true&amp;style=rotate&amp;send_services=email%2Caim%2Csms&amp;post_services=twitter%2Cfriendfeed%2Cfacebook%2Cdigg%2Cdelicious%2Creddit%2Cslashdot%2Cgoogle_bmarks%2Cblogger%2Ctypepad%2Cstumbleupon%2Cwordpress%2Cwindows_live%2Cnewsvine%2Clinkedin%2Cmyspace%2Ctechnorati"></script>
</div>

</div>
</div>
<!-- end  print-discuss-del.icio.us options -->





<!-- content here -->














































	<!-- mega preparse mp:yes -->

<h2>
Using JUnit With Eclipse IDE</h2>
by <a href="/pub/au/1361">Alexander Prohorenko</a> and <a href="/pub/au/1720">Olexiy Prokhorenko</a><br />
02/04/2004


<p>This article is going to introduce you to <a href="http://www.junit.org/">JUnit</a>, a tool for project testing and debugging. After introducing the theory of test-driven development, we'll move on to a step-by-step explanation of how you can create your JUnit tests with the help of the popular <a href="http://www.eclipse.org/">Eclipse  IDE</a>. We'll show how something as simple as a <i>Hello World</i> program can be exposed to a JUnit test.</p>

<!--ONJava MPU Ad -->

<div style="float:right;width:300px;padding-left:15px;">
<!--JavaScript Tag // Tag for network 5159: TechVertical // Website: OR - ONJava // Page: ONJava // Placement: ros_300x250 (425855) // created at: Oct 9, 2008 5:48:41 PM-->
<script language="javascript"><!--
document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5159/425855/0/170/ADTECH;loc=100;target=_blank;key=key1+key2+key3+key4;grp=[group];misc='+new Date().getTime()+'"></scri'+'pt>');
//-->
</script><noscript><a href="http://adserver.adtechus.com/adlink/3.0/5159/425855/0/170/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" target="_blank"><img src="http://adserver.adtechus.com/adserv/3.0/5159/425855/0/170/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" border="0" width="300" height="250"></a></noscript>
<!-- End of JavaScript Tag -->
</div>



<p>Many books have already been written about automated testing, but very few of them pay attention to the question of how to organize such tests. As more tests are written, it becomes harder to know where to put a test or even what to call it. This has become a significant issue with the rise of test-driven development (TDD), which has been popularized by <a href="http://www.extremeprogramming.org">Extreme Programming</a> (XP). You can think of TDD as "development through testing." </p>

<p>The major provisions of TDD are:</p>

<ul>
 <li>Before writing any code fragment, an automated test must be written to check the
  functioning of this code. Since the code does not exist yet, it initially fails
  the test. </li>
 <li>  After the test begins to pass, duplicate code must be removed. </li>
</ul>

<p>Such an approach can be used by any programmer and does not require the use of a
specific methodology. But before we get into writing tests, it would be desirable to first look at how to organize the automated tests.</p>

<p>There several different kinds of tests we should consider:</p>

<!--csinclude record="b/924" template="b/article_sidebar.view"-->

<ul>
 <li><p><b>Unit tests</b>: These serve to check the correctness of several modules (i.e., classes).
  If the object needs access to some external data source, like a database, these are
  simulated with <i>mock objects</i>, but only in cases in which re-creating the actual
  data source would be difficult in a test environment.</p></li>

 <li><p><b>Customer's tests</b>: These are functional, system, and acceptance tests.
  All of them check the behavior of the system as a whole. In the XP methodology, these 
  tests are written by the customer, given the program skeleton. </p></li>

 <li><p><b>Integration tests</b>: These are like a cross between the customer tests and unit tests.
  Integration tests help test the interaction of several levels of the application. Typically, mock 
  objects are not used in integration testing, which may increase testing time. Also,
  integration tests often demand the presence of a special test environment, such as a database 
  seeded with test data. Integration tests may also use special external libraries. An example of such a
  library for the integration of J2EE applications is <a href="http://jakarta.apache.org/cactus/">Cactus</a>.
  Explanation of these tests is going beyond of this article, and requires much detailed theory 
  information, so you just take it as information that such kind of tests exists.</p> </li>

 <li><p><b>Developer's tests</b>: These are just tests that developers use to verify whole code, new pieces of 
  code, and/or new functions. For each developer, it is important to generate new tests to check code 
  whenever possible. Organizing these tests can be as important as organizing the
  code base itself.</p> </li>
</ul>

<p>For the rest of this article, when I say "test," I will mean developer's test.</p>

<p>During development, a programmer sometimes asks himself or herself: is there a test for the
given behavior of the system and if it exists, where it can be found? A classic example is basic bug
fixing, in which the mistake is found, but not by automated tests. The sequence
of events that results from this situation might be: </p>

<ol>
 <li>Look for a test for the given functionality (it's probable that the test is already written,
  but contains a mistake). </li>
 <li>If such test is not present, or it does not cover a situation in which there is a mistake, we
  will need to write a new test that reveals it.</li>
 <li>Now we need to be convinced that the new test does not pass.</li>
 <li>Fix the bug.</li>
 <li>Run the test.</li>
 <li>Confirm that it passes.</li>
</ol>

<p>Certainly, variations of this process are possible, but the idea should be clear: you only correct a 
mistake when you have a test that reveals the mistake.</p>

<p>Now, let's consider how a developer would solve this situation.
To search through existing functionality tests:</p>

<ul>
 <li>We can take advantage of certain integrated development environments (IDEs) that allow us to search for places in which corrected classes and methods are used.</li>
 <li>Create a known error situation and inspect what fragment of code is in error.</li>
 <li> Last but not least, write the test and place it in one of the existing test classes. If you make a mistake arranging your test and duplicate test code, hopefully you or one of your colleagues will eventually notice the duplication and correct it.</li>
</ul>

<p>We are almost ready to create our test, so now we have to choose a name for our test. You 
could say, "It's not even a problem: just put the word "Test" before your class name, and that's it!" 
But not so fast! Let me just show how this approach can run into trouble:</p>

<ul>
 <li>When using TDD, the class or method to be tested may not exist yet. </li>
 <li>It's also possible that one test can cover several methods, or even several classes.</li>
</ul>

<p>These are just the most common problems; there are many more.</p>

<p>Let me offer one recommendation on naming your tests: the name of a test class should 
convey that this class is a test class, and indicate what exactly it checks, whether or not it
repeats the name of a tested class. That's easy. Don't worry if such a name turns out too long or
ugly. It describes itself, and that is the idea.</p>

<p>We will create our first test with the help of the JUnit tool in the Eclipse IDE. I
assume that you have already downloaded a recent version of this product, but if not, you can always get it
from the <a href="http://www.eclipse.org/">official site</a>. We need JUnit, which you can download from its <a href="http://www.junit.org/">official site</a>, too. Download it and unzip somewhere on your disk, where you are keeping your Java libraries.</p>

<p>Run Eclipse IDE. We will create a new workplace project, so click <b>File -> New -> Project</b>,
then choose <b>Java</b> and click <b>Next</b>. Type in a project name -- for example, 
<code>ProjectWithJUnit</code>. Click <b>Finish</b>. The new project will be generated in your IDE.
Let's configure our Eclipse IDE, so it will add the JUnit library to the build path. Click on <b>Project -> Properties</b>, select <b>Java Build Path</b>, <b>Libraries</b>, click <b>Add External JARs</b> and 
browse to directory where your JUnit is stored. Pick <i>junit.jar</i> and click <b>Open</b>. You will 
see that JUnit will appear on your screen in the list of libraries. By clicking <b>Okay</b> you will 
force Eclipse to rebuild all build paths.</p>

<p>We are ready to start developing our "Hello World" example. Let's follow TDD rules and create the test even 
before we have any kind of code. For the sake of having somewhere to start, we
will assume that our future code class will be named <code>HelloWorld</code> and that it 
will have  the method <code>say()</code>, which will return some <code>String</code> value ("Hello World," for example).</p>

<p>To create such a test, right-click on the <code>ProjectWithJUnit</code> title, select <b>New -> Other</b>, 
expand the "Java" selection, and choose <b>JUnit</b>. On the right column of the dialog,
choose <b>Test Case</b>, then click <b>Next</b>. This is illustrated by Figure 1.</p>

<p><img src="/onjava/2004/02/04/graphics/juie1ss.gif" alt="Figure 1" width="400" height="401" /><br />
<i>Figure 1. Creating a JUnit test in the Eclipse IDE</i></p>

<p>Type in the name of our yet-to-be written class <code>HelloWorld</code>
into the <b>Test class</b> field, and choose a name for our <b>Test case</b> -- for example, <code>TestThatWeGetHelloWorldPrompt</code> (yes, it looks too long, but it clearly indicates what it does.) Click on <b>Finish</b>.</p>

<p>The code for <i>TestThatWeGetHelloWorldPrompt.java</i> is as follows:</p>

<pre><code>import junit.framework.TestCase;

public class TestThatWeGetHelloWorldPrompt
    extends TestCase {
    public TestThatWeGetHelloWorldPrompt(
        String name) {
        super(name);
    }
    public void testSay() {
        HelloWorld hi = new HelloWorld();
        assertEquals("Hello World!", hi.say());
    }
    public static void main(String[] args) {
        junit.textui.TestRunner.run(
            TestThatWeGetHelloWorldPrompt.class);
    }
}</code></pre>

<p>This code is not complex; it's just a bit unusual. However, let's examine it in detail. We extend 
JUnit's <code>TestCase</code> class, which is defined in <a href="http://junit.sourceforge.net/javadoc/junit/framework/TestCase.html">JUnit's javadocs</a> as "a fixture to run multiple tests." JUnit also has <code>TestSuite</code>, which is a set of related test cases, but we will not work with in this article.</p>

<table id="page-break" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:left;"><tr><td><p class="secondary">
Pages: <b>1</b>, <a href="/pub/a/onjava/2004/02/04/juie.html?page=2">2</a>
</p></td><td><p class="secondary" align="right">
<a href="/pub/a/onjava/2004/02/04/juie.html?page=2">
Next Page<img src="/images/arrows/arrows-66.gif" border="0" alt="arrow" width="12" height="13" hspace="2" vspace="0" align="baseline"></a></p></td></tr></table>







<!-- end article content -->

<!-- OST promo -->

<!-- end O'Reilly Learning box -->



<!-- end content -->


<img src="/images/trans.gif" alt=" " width="400" height="1" /><br />



<br /><br />


<!-- end content -->

</div><!-- #article -->

<div id="related">


<div class="sidebar-item">
<h3>Tagged Articles</h3>
<div class="sidebar-item-content tags">
<p class="addtags"><a class="delicious" title="Post to del.icio.us." href="http://del.icio.us/post" onclick="window.open('http://del.icio.us/post?v=4&partner=orn&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title),'delicious','toolbar=no,width=700,height=400'); return false;">Post to del.icio.us</a><br /><br />
This article has been tagged:</p>

<div class="tabber">
<div class="tabbertab"><h2>eclipse</h2> <p style="padding-top:5px;">Articles that share the tag <em>eclipse</em>:</p><p><a href="http://www.onjava.com/pub/a/onjava/2005/02/09/eclipse.html">Eclipse Plugins Exposed, Part 1: A First Glimpse</a> <span class="tagcount">(156 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2006/08/16/profiling-with-eclipse-callisto.html">Profiling Your Applications with Eclipse Callisto</a> <span class="tagcount">(121 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/05/11/sqlexplorer.html">Configuring Database Access in Eclipse 3.0 with SQLExplorer</a> <span class="tagcount">(75 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2004/06/23/hibernate.html">Working with Hibernate in Eclipse</a> <span class="tagcount">(60 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/08/31/eclipse-jboss-remote-debug.html">Configuring Eclipse for Remote Debugging</a> <span class="tagcount">(49 tags)</span></p><p class="more"><a href="http://www.oreillynet.com/tags.csp?tag=eclipse">View All</a></p></div><div class="tabbertab"><h2>junit</h2> <p style="padding-top:5px;">Articles that share the tag <em>junit</em>:</p><p><a href="http://www.onjava.com/pub/a/onjava/2004/02/04/juie.html">Using JUnit With Eclipse IDE</a> <span class="tagcount">(21 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/01/12/mocquer.html">Mock Objects in Unit Tests</a> <span class="tagcount">(18 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/07/13/pisces.html">Taking JUnit Out of the Box</a> <span class="tagcount">(4 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html">Approaches to Mocking</a> <span class="tagcount">(2 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2004/01/21/dbunit.html">Effective Unit Testing with DbUnit</a> <span class="tagcount">(2 tags)</span></p><p class="more"><a href="http://www.oreillynet.com/tags.csp?tag=junit">View All</a></p></div><div class="tabbertab"><h2>java</h2> <p style="padding-top:5px;">Articles that share the tag <em>java</em>:</p><p><a href="http://www.onjava.com/pub/a/onjava/2006/08/16/profiling-with-eclipse-callisto.html">Profiling Your Applications with Eclipse Callisto</a> <span class="tagcount">(113 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/09/14/what-is-a-portlet.html">What Is a Portlet</a> <span class="tagcount">(101 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/01/12/xpath.html">Parsing an XML Document with XPath</a> <span class="tagcount">(79 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/04/27/restweb.html">The REST of the Web</a> <span class="tagcount">(75 tags)</span></p><p><a href="http://www.onjava.com/pub/a/onjava/2005/02/09/eclipse.html">Eclipse Plugins Exposed, Part 1: A First Glimpse</a> <span class="tagcount">(69 tags)</span></p><p class="more"><a href="http://www.oreillynet.com/tags.csp?tag=java">View All</a></p></div><div class="tabbertab"><h2>programming</h2> <p style="padding-top:5px;">Articles that share the tag <em>programming</em>:</p><p><a href="http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html">Rolling with Ruby on Rails</a> <span class="tagcount">(1374 tags)</span></p><p><a href="http://www.xml.com/pub/a/2005/02/09/xml-http-request.html">Very Dynamic Web Interfaces</a> <span class="tagcount">(279 tags)</span></p><p><a href="http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html">Ajax on Rails</a> <span class="tagcount">(231 tags)</span></p><p><a href="http://www.onlamp.com/pub/a/php/2005/09/15/mvc_intro.html">Understanding MVC in PHP</a> <span class="tagcount">(202 tags)</span></p><p><a href="http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html">A Simpler Ajax Path</a> <span class="tagcount">(186 tags)</span></p><p class="more"><a href="http://www.oreillynet.com/tags.csp?tag=programming">View All</a></p></div><div class="tabbertab"><h2>howto</h2> <p style="padding-top:5px;">Articles that share the tag <em>howto</em>:</p><p><a href="http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html">Rolling with Ruby on Rails</a> <span class="tagcount">(258 tags)</span></p><p><a href="http://www.onlamp.com/pub/a/onlamp/2006/04/20/from-weblog-to-cms.html">From Weblog to CMS with WordPress</a> <span class="tagcount">(98 tags)</span></p><p><a href="http://www.macdevcenter.com/pub/a/mac/2002/10/22/digi_photo_tips.html">Top Ten Digital Photography Tips</a> <span class="tagcount">(92 tags)</span></p><p><a href="http://www.macdevcenter.com/pub/a/mac/2002/10/22/macforunix.html">Top Ten Mac OS X Tips for Unix Geeks</a> <span class="tagcount">(79 tags)</span></p><p class="more"><a href="http://www.oreillynet.com/tags.csp?tag=howto">View All</a></p></div></div></div></div>

<!-- sponsored developer resources -->
<div class="sidebar-item">
<h3>Sponsored Resources</h3>
<div class="sidebar-item-content">

<ul style="margin: 0 0 0 1.2em;padding: 0;">
<li><a href="http://digitalmedia.oreilly.com/lightroom/">Inside Lightroom</a></li>
</ul>

</div>
</div>


<!-- csperl file="article_related_to" topic="25" / -->





<div style="text-align:center;background-color:#eee;padding-top:10px;padding-bottom:10px;">
<img src="/images/ads/advertisement.gif" alt="Advertisement" style="padding-bottom:10px;" /><br />

<div align="center">
<!--JavaScript Tag // Tag for network 5159: TechVertical // Website: OR - ONJava // Page: ONJava // Placement: ros_160x600 (425852) // created at: Oct 9, 2008 5:48:40 PM-->
<script language="javascript"><!--
document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5159/425852/0/154/ADTECH;loc=100;target=_blank;key=key1+key2+key3+key4;grp=[group];misc='+new Date().getTime()+'"></scri'+'pt>');
//-->
</script><noscript><a href="http://adserver.adtechus.com/adlink/3.0/5159/425852/0/154/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" target="_blank"><img src="http://adserver.adtechus.com/adserv/3.0/5159/425852/0/154/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]" border="0" width="160" height="600"></a></noscript>

<!-- End of JavaScript Tag --></div>


</div>


</div><!-- #related -->

</div><!-- #page-content -->

<br clear="all">

<!-- footer -->
<div id="footer" class="noindex">
  <table border="0" cellspacing="0" cellpadding="0" class="emailSignUpTable">
    <tbody>
      <tr>
        <td style="width:516px;">
		<form method="POST" id="example_form" action="http://post.oreilly.com/client/o/oreilly/forms/quicksignup.cgi">
          <table border="0" cellspacing="0" cellpadding="0">
            <tbody>
              <tr>
                <td style="width: 255px; padding:0 5px 0 12px;">
                  Sign up today to receive special discounts,<br>
                  product alerts, and news from O'Reilly.
                </td>
                <td style="width: 240px; vertical-align:bottom;">
                <ul class="utility">
            	<li>
					<input type="hidden" name="client_token" value="oreilly">
					<input type="hidden" name="subscribe" value="optin"> 
					<input type="hidden" name="success_url" value="http://oreilly.com/store/newsletter-thankyou.html">
					<input type="hidden" name="error_url" value="http://oreilly.com/store/newsletter-signup-error.html">
                	<input type="hidden" value="1" name="news_oreilly"> 
                	<input type="text" name="email_addr" value="Enter Email" style="width: 172px; vertical-align: top; height: 22px; padding:0 0 0 2px; border:0px;" maxlength="100" size="12"> </li>
                 <li>
                 	<input border="0" type="image" alt="Submit" src="http://oreilly.com/images/oreilly/emailsignup_btn.gif" type="submit" name="submit" value="submit" /></li>
                 </ul>
				</td>
              </tr>
            </tbody>
          </table>
          </form>
        </td>

        <td style="width: 162px;"><a href="http://oreilly.com/oreilly/privacy.html">Privacy Policy &gt;</a><br>
        <a href="http://oreilly.com/pdf/sample-newsletter.pdf" alt="View Sample Newsletter &gt;">View Sample Newsletter &gt;</a></td>

        <td style="text-align:right; width: 288px;">
          <ul class="utility">
            <li><a href="http://www.youtube.com/OreillyMedia" title="Youtube"><img src="http://oreilly.com//images/oreilly/youtube_footer.gif" alt="Youtube" align="absmiddle"></a></li>

            <li><a href="http://www.facebook.com/OReilly" title="Facebook"><img src="http://oreilly.com//images/oreilly/facebook_footer.gif" alt="http://www.youtube.com/OreillyMedia" align="absmiddle"></a></li>

            <li><a href="http://twitter.com/oreillymedia" title="Twitter"><img src="http://oreilly.com//images/oreilly/twitter_footer.gif" alt="Twitter" align="absmiddle"></a></li>

            <li><a href="http://feeds2.feedburner.com/oreilly/all" title="Subscribe"><img src="http://oreilly.com//images/oreilly/rss_footer.gif" alt="Subscribe" align="absmiddle"></a></li>

            <li style="padding-right:10px;"><a href="http://oreilly.com/feeds/" title="View All RSS Feeds &gt;">View All RSS Feeds &gt;</a></li>
          </ul>
        </td>
      </tr>
    </tbody>
 </table>

<table border="0" style="padding-top:20px; padding-bottom:20px;">
<tbody>
<tr><td align="left" width="25%" valign="top" style="padding-left: 20px; padding-right: 20px;">
<a href="http://oreilly.com/"><img height="35" border="0" width="155" src="http://cdn.oreilly.com/images/standard/oreilly-logo-footer.gif" alt="O'Reilly Media"/></a>
<br/><br/>
800-889-8969 or 707-827-7019 <br />
Monday-Friday 7:30am-5pm PT<br/> 
&copy;2011, O'Reilly Media, Inc.<br/>
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.</td>

<td valign="top" class="footercol1">
<ul>
<li><a href="http://oreilly.com/about/"><strong>About O'Reilly</strong></a></li>
<li><a href="http://oreilly.com/academic/">Academic Solutions</a></li>
<li><a href="http://oreilly.com/contact.html">Contacts</a></li>
<li><a href="http://oreilly.com/oreilly/cs/">Customer Service</a></li>
<li><a href="http://oreilly.com/jobs/">Careers</a></li>
<li><a href="http://press.oreilly.com/">Press Room</a></li>
<li><a href="http://oreilly.com/oreilly/privacy.csp">Privacy Policy</a></li>
<li><a href="http://oreilly.com/terms/">Terms of Service</a></li>
<li><a href="http://oreilly.com/oreilly/author/intro.csp">Writing for O'Reilly</a></li>
</ul>
</td>

<td valign="top" class="footercol2">
<ul>
<li><a href="http://oreilly.com/community/"><strong>Community</strong></a></li>
<li><a href="http://oreilly.com/authors/">Authors</a></li>
<li><a href="http://forums.oreilly.com/">Forums</a></li>
<li><a href="https://members.oreilly.com/">Membership</a></li>
<li><a href="http://elists.oreilly.com/">Newsletters</a></li>
<li><a href="http://oreilly.com/feeds/">RSS Feeds</a></li>
<li><a href="http://ug.oreilly.com/">User Groups</a></li>
</ul>
</td>

<td valign="top" class="footercol3">
<ul>
<li><strong>More O'Reilly Sites</strong></li>
<li><a href="http://igniteshow.com/">igniteshow.com</a></li>
<li><a href="http://makerfaire.com/">makerfaire.com</a></li>
<li><a href="http://makezine.com/">makezine.com</a></li>
<li><a href="http://craftzine.com">craftzine.com</a></li>
<li><a href="http://labs.oreilly.com">labs.oreilly.com</a></li>
</ul>

<ul>
<li><strong>Partner Sites</strong></li>
<li><a href="https://www.x.com/community/ppx/devzone">PayPal Developer Zone</a></li>
<li><a href="http://blogs.forbes.com/oreillymedia/">O'Reilly Insights on Forbes.com</a></li>
</ul>
</td>
</tr>
</tbody>
</table>


</div>

<!-- autocomplete scripts -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/yui/2.6.0/build/utilities/utilities.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/yui/2.6.0/build/datasource/datasource-min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/yui/2.6.0/build/autocomplete/autocomplete-min.js"></script>
<script type="text/javascript" src="http://content.atomz.com/sp1003bcf0/publish/autocomplete_data.js?sp_js_cache_ver=3"></script>

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>	

<!-- MyBuys Page Initialization -->
<script type="text/javascript">
mybuys.initPage();
</script>
<!-- End MyBuys Page Initialization -->

<!-- #footer -->
</div></body><!-- #page -->



</html>



